############################################################
#Relative Value Measure
#Figures OB1 & OB2
############################################################

library(rio)
library(tidyverse)
library(ggstance)


#####################
#Functions
####################

`%notin%` <- Negate(`%in%`)

m1_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 1")
}

m2_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 2")
}

m3_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 3")
}



#####################
#Vote
####################

v1 <- import("vote_elections1_rel.dta") %>% m1_func()
v2 <- import("vote_elections2_rel.dta") %>% m2_func()
v3 <- import("vote_elections3_rel.dta") %>% m3_func()

#####################
#Support Others
####################
s1 <- import("support_others1_rel.dta") %>% m1_func()
s2 <- import("support_others2_rel.dta") %>% m2_func()
s3 <- import("support_others3_rel.dta") %>% m3_func()

#####################
#Obey
####################
o1 <- import("obey1_rel.dta") %>% m1_func()
o2 <- import("obey2_rel.dta") %>% m2_func()
o3 <- import("obey3_rel.dta") %>% m3_func()

#####################
#Opinions
####################
op1 <- import("opinions1_rel.dta") %>% m1_func()
op2 <- import("opinions2_rel.dta") %>% m2_func()
op3 <- import("opinions3_rel.dta") %>% m3_func()

#####################
#Volunteer
####################

vo1 <- import("volunteer1_rel.dta") %>% m1_func()
vo2 <- import("volunteer2_rel.dta") %>% m2_func()
vo3 <- import("volunteer3_rel.dta") %>% m3_func()

#####################
#Be Active in Politics
####################

a1 <- import("beactive1_rel.dta") %>% m1_func()
a2 <- import("beactive2_rel.dta") %>% m2_func()
a3 <- import("beactive3_rel.dta") %>% m3_func()

#####################
#Combine (Export)
####################

comb <- bind_rows(v1, v2, v3, 
                  a1, a2, a3, 
                  o1, o2, o3, 
                  op1, op2, op3, 
                  s1, s2, s3,
                  vo1, vo2, vo3)

#####################
#Figure
####################

comb1 <- comb %>%
  filter(parm %in% c("self_relative", "cons_relative"))

comb1a <- comb1 %>%
  mutate(label = factor(label, 
                        levels=c("Self-Transcendence vs. Self-Enhancement", 
                                 "Conservation vs. Openness")), 
         model = factor(model, 
                        levels=c("Model 3", "Model 2", "Model 1")), 
         eq = factor(eq, 
                     levels=c("vote_elections", "beactive", "volunteer", 
                              "obey", "opinions", "support_others"), 
                     labels=c("Vote", "Be Active", "Volunteer", 
                              "Obey", "Opinions", "Support Others")))


#With labels
comb1a %>%
  ggplot(aes(x=estimate, y=model)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95)) + 
  facet_grid(eq ~ label, labeller = label_wrap_gen(width = 25)) + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = "Coefficient") + 
  geom_text(aes(label=round(estimate, 2)), 
            nudge_y=0.3) +
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold"))

ggsave("figure_ob1.png", height=10, width=12, dpi=600)

################################################################
# Figure OB2
##################################################################

rm(list=ls())

`%notin%` <- Negate(`%in%`)

m1_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 1")
}

m2_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 2")
}

m3_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 3")
}

#####################
#Vote
####################

v1 <- import("vote_elections1_rel.dta") %>% m1_func()
v2 <- import("vote_elections2_rel.dta") %>% m2_func()
v3 <- import("vote_elections3_rel.dta") %>% m3_func()

#####################
#Support Others
####################
s1 <- import("support_others1_rel.dta") %>% m1_func()
s2 <- import("support_others2_rel.dta") %>% m2_func()
s3 <- import("support_others3_rel.dta") %>% m3_func()

#####################
#Obey
####################
o1 <- import("obey1_rel.dta") %>% m1_func()
o2 <- import("obey2_rel.dta") %>% m2_func()
o3 <- import("obey3_rel.dta") %>% m3_func()

#####################
#Opinions
####################
op1 <- import("opinions1_rel.dta") %>% m1_func()
op2 <- import("opinions2_rel.dta") %>% m2_func()
op3 <- import("opinions3_rel.dta") %>% m3_func()

#####################
#Volunteer
####################

vo1 <- import("volunteer1_rel.dta") %>% m1_func()
vo2 <- import("volunteer2_rel.dta") %>% m2_func()
vo3 <- import("volunteer3_rel.dta") %>% m3_func()

#####################
#Be Active in Politics
####################

a1 <- import("beactive1_rel.dta") %>% m1_func()
a2 <- import("beactive2_rel.dta") %>% m2_func()
a3 <- import("beactive3_rel.dta") %>% m3_func()

#####################
#Combine (Export)
####################

comb <- bind_rows(v1, v2, v3, 
                  a1, a2, a3, 
                  o1, o2, o3, 
                  op1, op2, op3, 
                  s1, s2, s3,
                  vo1, vo2, vo3)


#####################
#Figure
####################

comb1a <- comb %>%
  filter(model == "Model 3") %>%
  filter(parm %notin% c("2.edulvla", "3.edulvla", "4.edulvla", 
                        "0.edulvlfa", "2.edulvlfa", "3.edulvlfa", "4.edulvlfa", 
                        "3.born_when1")) %>%
  mutate(parm = factor(parm, 
                       levels=c("self_relative", "cons_relative", 
                                "age", "1.gender", 
                                "1.minority1", "1.union1", 
                                "5.edulvla", "5.edulvlfa", 
                                "2.born_when1", "socialtrust", "sclmeet", 
                                "religiosity", "yrlvdae"), 
                       labels=c("Self-Transcendence", "Conservation", 
                                "Age", "Female", 
                                "Minority", "Union Mmbr.", 
                                "Tertiary Ed (Self)", "Tertiary Ed (Father)", 
                                "Came to Cntry <20yrs Ago", "Interpersonal Trust", 
                                "Social Engagement", "Religiosity", "Res. Stability")), 
         eq = factor(eq, 
                     levels=c("vote_elections", "beactive", "volunteer", 
                              "obey", "opinions", "support_others"), 
                     labels=c("Vote", "Be Active", "Volunteer", 
                              "Obey", "Opinions", "Support Others")), 
         values = ifelse(parm %in% c("Self-Transcendence", "Conservation"), 1, 0), 
         values = factor(values, levels=c(1,0), 
                         labels=c("Values", "Controls")))



ggplot(comb1a, aes(x=estimate, y=parm)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95)) + 
  facet_grid(values ~ eq, labeller = label_wrap_gen(width = 25), scales="free", space="free") + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = "Coefficient") + 
  geom_text(aes(label=round(estimate, 2)), 
            nudge_y=0.20) +
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold")) 

ggsave("figure_ob2.png", height=8, width=12, dpi=600)




